package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Email;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.service.IEmailService;
import cn.wolfcode.crm.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("email")
public class EmailController {
    @Autowired
    private IEmailService emailService;

    @RequestMapping("view")
    public String view() {
        return "email/email";
    }

    @RequestMapping("queryLinkman")
    @ResponseBody
    public List<Email> queryLinkman() {
        return emailService.queryLinkman();
    }

    @RequestMapping("queryColleague")
    @ResponseBody
    public List<Employee> queryColleague() {
        return emailService.queryColleague();
    }

    @RequestMapping("queryEmailMessage")
    @ResponseBody
    public Email queryEmailMessage(Long sourceId, Long id) {
        System.out.println("sourceId:" + sourceId);
        System.out.println("id:" + id);
        if (sourceId == null || id == null) {
            return null;
        }
        return emailService.queryEmailMessage(sourceId, id);
    }

    @RequestMapping("sendMessage")
    @ResponseBody
    public JsonResult sendMessage(Email email) {
        JsonResult jsonResult = new JsonResult();
        try {
            emailService.sendMessage(email);
            return jsonResult;
        } catch (Exception e) {
            return jsonResult.mark("发送失败,原因:" + e.getMessage());

        }
    }
    @RequestMapping("deleteMessage")
    @ResponseBody
    public JsonResult deleteMessage(Email email) {
        JsonResult jsonResult = new JsonResult();
        try {
            emailService.deleteMessage(email);
            return jsonResult;
        } catch (Exception e) {
            return jsonResult.mark("发送失败,原因:" + e.getMessage());

        }
    }
}
